<?php

namespace app\model\order;

use app\base\BaseModel;

/**
 * 商品拼团记录
 * 模型-OrderGroupModel
 * Class OrderGroupModel
 * @package app\model
 */
class OrderGroupModel extends BaseModel
{
    protected $name = 'order_group';

    /**
     * 添加拼团记录
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addGroup($data)
    {
        $map = [
            'goods_id' => $data['goods_id'],
            'user_id' => $data['user_id'],
            'status' => 1
        ];
        $temp_data = $this->where($map)->find();
        if (empty($temp_data)) {
            $ret = $this->save($data);
            if ($ret !== false) {
                $this->isEndGroup($data['goods_id'], $data['goods_group']);
                return true;
            }
        } else {
            return false;
        }
        return false;
    }

    /**
     * 根据商品获取拼团情况
     * @param $goods_id
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGroup($goods_id)
    {
        $map = [
            'goods_id' => $goods_id,
            'status' => 1
        ];
        $return_data = [];
        $group_list = $this->where($map)->select();
        if (empty($group_list)) {
            return $return_data;
        }
        foreach ($group_list as $value) {
            $return_data[$value['goods_group']]['items'][] = [
                'goods_group' => $value['goods_group'],
                'order_id' => $value['order_id'],
                'user_id' => $value['user_id'],
                'nick_name' => $value['nick_name'],
                'head_img' => $value['head_img']
            ];
            $return_data[$value['goods_group']]['goods_group'] = $value['goods_group'];
            $return_data[$value['goods_group']]['count'] = $value['count'];
            $return_data[$value['goods_group']]['time'] = $value['end_time'];
            $return_data[$value['goods_group']]['time_arr'] = $this->_getTimeStr($value['end_time'] - time());
            if (isset($return_data[$value['goods_group']]['total'])) {
                $return_data[$value['goods_group']]['total'] = intval($return_data[$value['goods_group']]['total']) + 1;
            } else {
                $return_data[$value['goods_group']]['total'] = 1;
            }
        }
        $order_model = new OrderModel();
        foreach ($return_data as $key => $value) {
            if ($value['time'] < time() && intval($value['total']) != intval($value['count'])) {
                $order_model->cancelGroupOrder($value);
                unset($return_data[$key]);
            }
        }
        return $return_data;
    }


    /**判断是否有未支付订单 如果存在 返回订单号
     * @param $goods_id
     * @param $user_id
     * @return mixed|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function isHasPay($goods_id, $user_id)
    {
        $this->isExpiresGroup($goods_id);
        $map = [
            'user_id' => $user_id,
            'goods_id' => $goods_id,
            'status' => 0
        ];
        $group = $this->where($map)->find();
        if (empty($group)) {
            return "";
        }
        return $group['order_id'];
    }

    /**支付成功回调处理
     * @param $goods_group
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function endOrderPay($goods_group)
    {
        $map = [
            'goods_group' => $goods_group,
            'status' => 0
        ];
        $up_data = [
            'status' => 1,
        ];
        $ret = $this->where($map)->update($up_data);
        if ($ret !== false) {
            return true;
        }
        return false;
    }

    /**
     * 是否完成拼团
     * @param $goods_id
     * @param $goods_group
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function isEndGroup($goods_id, $goods_group)
    {
        $map = [
            'goods_group' => $goods_group,
            'status' => 1
        ];
        if (!empty($goods_id)) {
            $map['goods_id'] = $goods_id;
        }

        $group_list = $this->where($map)->select();
        $total = 0;
        $count = 0;
        foreach ($group_list as $value) {
            $total++;
            $count = $value['count'];
        }
        if ($total > 0 && ($total == $count)) {
            $order_model = new OrderModel();
            $order_model->endGorupOrder($goods_group);
            return true;
        }
        return false;
    }

    /**
     * 判断团购是否超时
     * @param $goods_id
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function isExpiresGroup($goods_id)
    {
        $group_list = $this->getGroup($goods_id);
        $order_model = new OrderModel();
        foreach ($group_list as $key => $value) {
            if ($value['time'] < time() && intval($value['total']) != intval($value['count'])) {
                $order_model->cancelGroupOrder($value);
            }
        }
    }

    private function _getTimeStr($time)
    {

        $d = intval($time / (3600 * 24));
        $h = intval(($time % (3600 * 24)) / 3600);
        $m = intval(($time % 3600) / 60);
        $s = intval($time % 60);
        return [
            'd' => $d,
            'h' => $h,
            'm' => $m,
            's' => $s,
        ];
    }
}
